//
//  PPAlbumPictureInsertionTableViewPicturesCell.m
//  pickpic
//
//  Created by Jinxiao on 10/2/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumPictureInsertionTableViewPicturesCell.h"

#import "PPAlbumContentImageView.h"

#define kEdgeInsets UIEdgeInsetsMake(13, 14, 13, 8)
#define kAddHeight 135
#define kSpacing 3
#define kSpacing_Add_Image 10

@interface PPAlbumPictureInsertionTableViewPicturesCell ()
@property (readwrite, nonatomic, strong) UIButton *addButton;
@property (readwrite, nonatomic, strong) NSArray *imageViews;
@end

@implementation PPAlbumPictureInsertionTableViewPicturesCell

- (void)awakeFromNib {
    [super awakeFromNib];

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addButton setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithRGBHex:0xffdd17]] stretchableImageByCenter] forState:UIControlStateNormal];
    [_addButton setImage:[UIImage imageNamed:@"icon-add-white"] forState:UIControlStateNormal];
    [self addSubview:_addButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _addButton.frame = CGRectMake(kEdgeInsets.left, kEdgeInsets.top, self.width - kEdgeInsets.left - kEdgeInsets.right, kAddHeight);

    [_imageViews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    NSMutableArray *imageViews = [NSMutableArray array];

    for(NSInteger index = 0; index < _dataSource.images.count; index++) {
        PPAbstractArtwork *artwork = _dataSource.images[index];

        PPAlbumContentImageView *imageView = [[PPAlbumContentImageView alloc] init];
        imageView.artwork = artwork;
        [self addSubview:imageView];

        [imageViews addObject:imageView];

        __weak typeof(self)weakSelf = self;
        [imageView.deleteButton setActionBlock:^(UIControl *control) {
            [weakSelf.delegate picturesCell:weakSelf didSelectDeleteButton:(UIButton *)control atIndex:index];
        } forControlEvents:UIControlEventTouchUpInside];

        [imageView.control setActionBlock:^(UIControl *control) {
            [weakSelf.delegate albumCell:weakSelf didSelectArtwork:artwork atIndex:index];
        } forControlEvents:UIControlEventTouchUpInside];
    }

    _imageViews = imageViews;

    for(NSInteger index = 0; index < _imageViews.count; index++) {
        PPAlbumContentImageView *imageView = _imageViews[index];
        imageView.frame = [[self class] tileFrameForIndex:index boundingSize:CGSizeMake(self.width, CGFLOAT_MAX)];
    }
}

- (void)setDataSource:(PPAlbumContentViewDataSource *)dataSource {
    _dataSource = dataSource;

    [self setNeedsLayout];
}

+ (CGFloat)heightForNumberOfPictures:(NSInteger)numberOfPictures boundingSize:(CGSize)boundingSize {

    if(numberOfPictures == 0) {
        return kAddHeight + kEdgeInsets.top + kEdgeInsets.bottom;
    } else {
        CGSize tileSize = [self tileSizeForBoundingSize:boundingSize];

        NSInteger rows = ceil(numberOfPictures/4.0);

        if(rows == 1) {
            return kAddHeight + kEdgeInsets.top + kEdgeInsets.bottom + kSpacing_Add_Image + tileSize.height;
        } else {
            return kAddHeight + kEdgeInsets.top + kEdgeInsets.bottom + kSpacing_Add_Image + tileSize.height*rows + kSpacing*(rows - 1);
        }
    }
}

+ (CGRect)tileFrameForIndex:(NSInteger)index boundingSize:(CGSize)boundingSize {
    CGSize tileSize = [self tileSizeForBoundingSize:boundingSize];

    NSInteger row = floor(index/4.0);
    NSInteger column = index%4;

    return CGRectMake(kEdgeInsets.left + column*(tileSize.width + kSpacing), kAddHeight + kEdgeInsets.top + kSpacing_Add_Image + row*(tileSize.height + kSpacing), tileSize.width, tileSize.height);
}

+ (CGSize)tileSizeForBoundingSize:(CGSize)boundingSize {
    CGFloat width = (boundingSize.width - kEdgeInsets.left - kEdgeInsets.right - kSpacing*3)/4;
    CGFloat height = width;
    
    return CGSizeMake(width, height);
}

@end
